package org.misalen.web.server.controllers.driver;

import java.util.Date;

import org.misalen.annotations.ControllerApi;
import org.misalen.annotations.InterfaceApi;
import org.misalen.utils.TextUtil;
import org.misalen.web.database.model.mysql.pro.ProDriverUser;
import org.misalen.web.server.config.ErrorCode;
import org.misalen.web.server.config.ServerResult;
import org.misalen.web.server.config.ServerUrl;
import org.misalen.web.server.config.Uncertain;
import org.misalen.web.server.controllers.BaseController;
import org.misalen.web.server.exception.CustomerException;
import org.misalen.web.server.forms.driver.DriverFindPwdForm;
import org.misalen.web.server.forms.driver.DriverLoginAutoForm;
import org.misalen.web.server.forms.driver.DriverLoginForm;
import org.misalen.web.server.forms.driver.DriverModifyForm;
import org.misalen.web.server.forms.driver.DriverRegisteredForm;
import org.misalen.web.server.util.FormUtil;
import org.misalen.web.service.push.PushParameter;
import org.misalen.web.service.push.PushType;
import org.misalen.web.service.sms.info.AliInfoType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ControllerApi(value = "用户访问中心", description = "用户操作和信息", index = 200)
@RequestMapping(value = ServerUrl.DriverUserCenterControllerUrl)
public class DriverUserBehaviorController extends BaseController {

	@RequestMapping(value = "/registered", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "用户注册", resultClass = ProDriverUser.class)
	public @ResponseBody ServerResult registered(@RequestBody DriverRegisteredForm form) throws CustomerException {
		ServerResult result = null;
		FormUtil.validate(form);
		long count = proDriverUserRepository.countByMobile(form.getMobile());
		if (count == 0) {
			long sendByTimeCount = proSmsCodeRepository.countMobileTimeAndCode(form.getMobile(), form.getSmsCode(),
					Uncertain.smsValidity, AliInfoType.driverRegisteredCode.type);
			if (sendByTimeCount == 0) {
				result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_10003);
			} else {
				ProDriverUser proDriverUser = new ProDriverUser();
				proDriverUser.setAddTime(new Date());
				proDriverUser.setLoginTime(new Date());
				proDriverUser.setMobile(form.getMobile());
				proDriverUser.setImei(form.getImei());
				proDriverUser.setPassword(TextUtil.MD5(form.getPassword()));
				proDriverUser.setUserName(form.getUserName());
				proDriverUser.setBelongCity(form.getBelongCity());
				proDriverUser.setAgeRange(form.getAgeRange());
				proDriverUser.setSocialGroup(form.getSocialGroup());
				proDriverUser.setVehicleModel(form.getVehicleModel());
				proDriverUser.setVehicleNumber(form.getVehicleNumber());
				proDriverUser.setFreezeBalance(0l);
				proDriverUser.setBalance(0l);
				proDriverUser.setTodayIncome(0l);
				proDriverUser.setWithdrawal(0l);
				proDriverUser.setHeadrestCode(form.getHeadrestCode());
				proDriverUser.setState(TextUtil.booleanToString(true));
				proDriverUser.setBackseatCode(form.getBackseatCode());
				proDriverUserRepository.save(proDriverUser);
				PushParameter pushParameter = new PushParameter().setNotification(false).setTitle("").setType(PushType.driverRegistration.name());
				if(!TextUtil.isNullOrEmpty(form.getHeadrestCode())) {
					pushParameter.addToken(TextUtil.pustText(form.getHeadrestCode()));
				}
				if(!TextUtil.isNullOrEmpty(form.getBackseatCode())) {
					pushParameter.addToken(TextUtil.pustText(form.getBackseatCode()));
				}
				jPushImpl.sendPush(pushParameter);
				result = ServerResult.ok(proDriverUser);
			}
		} else {
			result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_10004);
		}

		return result;
	}
	@RequestMapping(value = "/update", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "用户注册", resultClass = ProDriverUser.class)
	public @ResponseBody ServerResult update(@RequestBody DriverModifyForm form) throws CustomerException {
		ServerResult result = null;
		FormUtil.validate(form);
		ProDriverUser proDriverUser = proDriverUserRepository.findByMobile(form.getMobile());
		if (proDriverUser == null) {
			result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_00005);
		} else {
			proDriverUser.setUserName(form.getUserName());
			proDriverUser.setVehicleModel(form.getVehicleModel());
			proDriverUser.setVehicleNumber(form.getVehicleNumber());
			String headrestCode = proDriverUser.getHeadrestCode();
			proDriverUser.setHeadrestCode(form.getHeadrestCode());
			String backseatCode = proDriverUser.getBackseatCode();
			proDriverUser.setBackseatCode(form.getBackseatCode());
			proDriverUserRepository.update(proDriverUser);
			
			if(!TextUtil.isSameString(headrestCode, form.getHeadrestCode()) || !TextUtil.isSameString(backseatCode, form.getBackseatCode())) {

				PushParameter pushParameter = new PushParameter().setNotification(false).setTitle("").setType(PushType.driverRegistration.name());
				if(!TextUtil.isNullOrEmpty(form.getHeadrestCode()) && !TextUtil.isSameString(headrestCode, form.getHeadrestCode())) {
					pushParameter.addToken(TextUtil.pustText(form.getHeadrestCode()));
				}
				if(!TextUtil.isNullOrEmpty(form.getBackseatCode()) && !TextUtil.isSameString(backseatCode, form.getBackseatCode())) {
					pushParameter.addToken(TextUtil.pustText(form.getBackseatCode()));
				}
				jPushImpl.sendPush(pushParameter);
			}
			result = ServerResult.ok(proDriverUser);
		}

		return result;
	}
	
	@RequestMapping(value = "/login", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "用户登录", resultClass = ProDriverUser.class)
	public @ResponseBody ServerResult login(@RequestBody DriverLoginForm form) throws CustomerException {
		ServerResult result = null;
		FormUtil.validate(form);
		ProDriverUser proDriverUser = proDriverUserRepository.findByMobilePwd(form.getMobile(), TextUtil.MD5(form.getPassword()));
		if (proDriverUser != null) {
			proDriverUser.setImei(form.getImei());
			proDriverUser.setLoginTime(new Date());
			proDriverUserRepository.update(proDriverUser);
			result = ServerResult.ok(proDriverUser);
		} else {
			result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_10006);
		}
		return result;
	}

	@RequestMapping(value = "/automatic/login", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "自动登录", resultClass = ProDriverUser.class)
	public @ResponseBody ServerResult automatic(@RequestBody DriverLoginAutoForm form) throws CustomerException {
		ServerResult result = null;
		FormUtil.validate(form);
		ProDriverUser ProDriverUser = proDriverUserRepository.findByMobileImei(form.getMobile(), form.getImei());
		if (ProDriverUser != null) {
			result = ServerResult.ok(ProDriverUser);
		} else {
			result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_10007);
		}
		return result;
	}

	@RequestMapping(value = "/findpwd", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "找回密码", resultClass = ProDriverUser.class)
	public @ResponseBody ServerResult findpwd(@RequestBody DriverFindPwdForm form) throws CustomerException {
		ServerResult result = null;
		FormUtil.validate(form);
		ProDriverUser ProDriverUser = proDriverUserRepository.findByMobile(form.getMobile());
		if (ProDriverUser != null) {
			long sendByTimeCount = proSmsCodeRepository.countMobileTimeAndCode(form.getMobile(), form.getSmsCode(),
					Uncertain.smsValidity, AliInfoType.driverFindPwdCode.type);
			if (sendByTimeCount == 0) {
				result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_10003);
			} else {
				ProDriverUser.setPassword(TextUtil.MD5(form.getPassword()));
				proDriverUserRepository.update(ProDriverUser);
				result = ServerResult.ok();
			}
		} else {
			result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_10005);
		}

		return result;
	}
}
